//
//  UIColor+Extension.swift
//  Test
//
//  Created by ZWJY on 2021/8/20.
//

import Foundation
import UIKit

// TODO: - 常用颜色处理
public extension UIColor {
    
    /// 主题蓝色
    class var color_sel_blue: UIColor? { UIColor.init(hexString: "#195BE8") }
    class var color_nor_blue: UIColor? { UIColor.init(hexString: "#82B3F2") }
    class var color_bg_blue: UIColor? { UIColor.init(hexString: "#CDEBFF") }
    
    /// 主题绿色
    class var color_sel_green: UIColor? { UIColor.init(hexString: "#008355") }
    class var color_nor_green: UIColor? { UIColor.init(hexString: "#6DC6A4") }
    class var color_bg_green: UIColor? { UIColor.init(hexString: "#CAF1D9") }
    
    /// 主题橙色
    class var color_sel_orange: UIColor? { UIColor.init(hexString: "#C35300") }
    class var color_nor_orange: UIColor? { UIColor.init(hexString: "#E6AE77") }
    class var color_bg_orange: UIColor? { UIColor.init(hexString: "#FEEDCC") }
    
    // 主题紫色
    class var color_sel_purple: UIColor? { UIColor.init(hexString: "#5700C3") }
    class var color_nor_purple: UIColor? { UIColor.init(hexString: "#9177E6") }
    class var color_bg_purple: UIColor? { UIColor.init(hexString: "#CCD1FE") }

    /// 背景色
    class var color_eee: UIColor? { UIColor.init(hexString: "#EEEEEE") }
    /// 背景色
    class var color_efef: UIColor? { UIColor.init(hexString: "#EFEFEF") }
    /// 333333
    class var color_333: UIColor? { UIColor.init(hexString: "#333333") }
    /// 666666
    class var color_666: UIColor? { UIColor.init(hexString: "#666666")}
    /// 999999
    class var color_999: UIColor? { UIColor.init(hexString: "#999999")}

    /// 白-黑
    class var color_white_black: UIColor? { UIColor.color(light: .white, dark: .black)}

    /// 随机色
    class var randomColor: UIColor? {
        let r: CGFloat = CGFloat(arc4random_uniform(256))
        let g: CGFloat = CGFloat(arc4random_uniform(256))
        let b: CGFloat = CGFloat(arc4random_uniform(256))
        return UIColor.init(r: r, g: g, b: b)
    }

    /// rgb 创建色值
    convenience init(r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat = 1.0) {
        self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha)
    }

    /// 16进制创建色值
    convenience init?(hexString: String, alpha: CGFloat = 1.0) {
        let color = UIColor.hexStringToColorRGB(hexString: hexString)
        guard let r = color.r, let g = color.g, let b = color.b else {
            #if DEBUG
            assert(false, "不是十六进制值")
            #endif
            return nil
        }
        self.init(r: r, g: g, b: b)
    }

    // MARK: 3.1、根据 十六进制字符串 颜色获取 RGB，如：#3CB371 或者 ##3CB371 -> 60,179,113
    /// 根据 十六进制字符串 颜色获取 RGB
    /// - Parameter hexString: 十六进制颜色的字符串，如：#3CB371 或者 ##3CB371 -> 60,179,113
    /// - Returns: 返回 RGB
    static func hexStringToColorRGB(hexString: String) -> (r: CGFloat?, g: CGFloat?, b: CGFloat?) {
        // 1、判断字符串的长度是否符合
        guard hexString.count >= 6 else {
            return (nil, nil, nil)
        }
        // 2、将字符串转成大写
        var tempHex = hexString.uppercased()
        // 检查字符串是否拥有特定前缀
        // hasPrefix(prefix: String)
        // 检查字符串是否拥有特定后缀。
        // hasSuffix(suffix: String)
        // 3、判断开头： 0x/#/##
        if tempHex.hasPrefix("0x") || tempHex.hasPrefix("##") {
            tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 2)..<tempHex.endIndex])
        }
        if tempHex.hasPrefix("#") {
            tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 1)..<tempHex.endIndex])
        }
        // 4、分别取出 RGB
        // FF --> 255
        var range = NSRange(location: 0, length: 2)
        let rHex = (tempHex as NSString).substring(with: range)
        range.location = 2
        let gHex = (tempHex as NSString).substring(with: range)
        range.location = 4
        let bHex = (tempHex as NSString).substring(with: range)
        // 5、将十六进制转成 255 的数字
        var r: UInt64 = 0, g: UInt64 = 0, b: UInt64 = 0
        Scanner(string: rHex).scanHexInt64(&r)
        Scanner(string: gHex).scanHexInt64(&g)
        Scanner(string: bHex).scanHexInt64(&b)
        return (r: CGFloat(r), g: CGFloat(g), b: CGFloat(b))
    }
}

extension UIColor {

    class func color(light: UIColor, dark: UIColor) -> UIColor{
        // iOS13以下
        guard #available(iOS 13.0, *) else {
            return light
        }

        // iOS13以上
        let color = UIColor.init{ traitCollection in
            if traitCollection.userInterfaceStyle == .light {
                return light
            }else {
                return dark
            }
        }
        return color
    }

    class func colorHexString(light: String, lightAlpha: CGFloat = 1, dark: String, darkAlpha: CGFloat = 1) -> UIColor?{
        // iOS13以下
        guard #available(iOS 13.0, *) else {
            return UIColor.init(hexString: light, alpha: lightAlpha)
        }

        // iOS13以上
        let color = UIColor.init{ traitCollection in
            if traitCollection.userInterfaceStyle == .light {
                return UIColor.init(hexString: light, alpha: lightAlpha) ?? .white
            }else {
                return UIColor.init(hexString: dark, alpha: darkAlpha) ?? .black
            }
        }
        return color
    }
}
